﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace POSPlus.EZBarTender
{
    /*
    public class QueryQueue
    {
        private Queue<string> _queries;
        private SqlConnection _sqlConnection;
        private SqlCommand _sqlCommand;
        private bool _working;

        private BackgroundWorker _queryWorker;
        private ProgressBar _progressBar;

        public QueryQueue(string sqlConnectionString, ProgressBar progressBar)
        {
            _queries = new Queue<string>();
            _sqlConnection = new SqlConnection(sqlConnectionString);
            _progressBar = progressBar;
            _queryWorker = new BackgroundWorker();
            _queryWorker.WorkerReportsProgress = true;
            _queryWorker.WorkerSupportsCancellation = true;
            _queryWorker.DoWork += new DoWorkEventHandler(_queryWorker_DoWork);
            _queryWorker.ProgressChanged += new ProgressChangedEventHandler(_queryWorker_ProgressChanged);
            _queryWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_queryWorker_RunWorkerCompleted);
        }

        public void StartWork()
        {
            if (!_working)
            {
                _working = true;
                _queryWorker.RunWorkerAsync();
            }
        }

        public void Cancel()
        {
            _queries.Clear();
            _queryWorker.CancelAsync();
        }

        void _queryWorker_DoWork(object sender, DoWorkEventArgs args)
        {
            string query = "";
            while (_queries.Count > 0)
            {
                try
                {
                if (_queryWorker.CancellationPending)
                {
                    args.Cancel = true;
                    return;
                }

                query = _queries.Dequeue();
                    _sqlCommand = new SqlCommand(query, _sqlConnection);
                    _sqlConnection.Open();
                    _sqlCommand.ExecuteNonQuery();
                    _sqlConnection.Close();
                    _queryWorker.ReportProgress(_queries.Count);
                }
                catch (Exception ex)
                {
                    DebugBox.Show(_sqlCommand.CommandText + "               \n" + ex.Message);
                    args.Cancel = true;
                    return;
                }
            }
        }

        void _queryWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            _progressBar.Value = e.ProgressPercentage;
        }

        void _queryWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _progressBar.Value = 0;
            _working = false;
        }

        public void AddQuery(string query)
        {
            _queries.Enqueue(query);
            StartWork();
        }
    }
     */
}
